package com.hdit.guimei.service.impl;

import com.hdit.guimei.domain.Cart;
import com.hdit.guimei.domain.Goods;
import com.hdit.guimei.domain.Paygoods;
import com.hdit.guimei.domain.Payment;
import com.hdit.guimei.mapper.CartMapper;
import com.hdit.guimei.mapper.PaymentMapper;
import com.hdit.guimei.service.CartService;
import com.sun.deploy.panel.ITreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @基本功能:
 * @ClassName: CartServiceImpl
 * @Description: TODO
 * @Author: 李嘉明
 * @Date: 2021/1/18 10:21
 * @Version 1.0
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    public CartMapper cartMapper;
    @Autowired
    public PaymentServiceImpl paymentService;
    /**
     *@methodName  findcartgoods
     *@方法功能描述：查询
     *@params  [id]
     *@return  java.util.List<com.hdit.guimei.domain.Cart>
     *@author   张宝玉
     *@Date    2021/1/19 10:27
     */
    @Override
    public List<Cart> findcartgoods(int id) {
        List<Cart> list = cartMapper.findcartgoods(id);
        return list;
    }
    /**
     *@methodName  payMoney
     *@方法功能描述：购物车 购物 并且改变购物车商品的状态
     *@params  [id, good_id, buy_num]
     *@return  double
     *@author   张宝玉
     *@Date    2021/1/19 14:10
     */
    @Override
    public double payMoney(Paygoods paygoods) {
        List<Cart> list = paygoods.getCartList();
        double money = 0;
        for (int i = 0; i < list.size(); i++) {
            double goodsprice = cartMapper.findgoodsprice( list.get(i).getGood_id());
            double a = goodsprice*list.get(i).getCart_count();//计算一种商品价值
            money +=a;//计算购物车需要的钱
            cartMapper.updatecartgoodnum(list.get(i).getId(),list.get(i).getCart_count());//更新数量

            //购物车购物要添加支付到支付表  传进的参数是payment实体类
            //把这个商品加入到 支付表中
            Payment payment = new Payment();
            payment.setPayment_type(paygoods.getPay_type());
            payment.setPayment_good(list.get(i).getGood_name());
            payment.setPaygoodnum(list.get(i).getCart_count());
            payment.setPaymoney(a);
            paymentService.addpeyment(payment);
        }
        return money;
    }
    /**
     *@methodName  delecartgoods
     *@方法功能描述：输入商品id 批量删除 修改状态
     *@params  [id, good_id]
     *@return  int
     *@author   张宝玉
     *@Date    2021/1/19 14:32
     */
    @Override
    public int delecartgoods(int id, int good_id) {
        cartMapper.updatecartgoodstate(id,good_id);
        return 0;
    }
}
